package com.eqdd.yiqidian.ui.gongneng.renshi.contract;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.ApplyContractActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

public class ApplyContractActivity extends SlimBaseMultiEditActivity {
    private ApplyContractActivityCustom dataBinding;
    //    @ToSave
    Boolean isFirstShow = false;
    //    @ToSave
    Boolean isSecondShow = false;
    //    @ToSave
    Boolean isThirdShow = false;
    //    @ToSave
    SlimEditEnterBean xingshiBean;
    //    @ToSave
    SlimEditEnterBean shiyongqiBean;
    //    @ToSave
    SlimEditEnterBean xingzhiBean;
    //    @ToSave
    SlimEditEnterBean cishuBean;
    //    @ToSave
    SlimEditEnterBean jieshuBean;
    //    @ToSave
    SlimEditTimeBean startTimeBean;
    //    @ToSave
    SlimEditTimeBean endTimeBean;
    //    @ToSave
    SlimEditEnterBean bankBean;
    private ActionSheetDialog leixingDialog;
    private ActionSheetDialog xingshiDialog;
    private ActionSheetDialog shiyongqiDialog;
    private ActionSheetDialog xingzhiDialog;
    private ActionSheetDialog cishuDialog;
    private ActionSheetDialog jieshuDialog;
    private ActionSheetDialog bankDialog;
    private String msg;
    //    @ToSave
    EmploymentListResultBean bean;
    private SlimEditEnterBean salaryBean;


    @Override
    public void initBinding() {
        isRead = false;
        isSave = false;
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_laodong_hetong);
        initTopTitleBar(View.VISIBLE, "发起劳动合同");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_CONTRACT)
                        .params("userGuid", user.getGuid())
                        .params("companyId", user.getCompanyId())
                        .params("signatory", bean.getGuid())
                        .params("signDepartId", bean.getDepartId())
                        .params("signPostId", bean.getPostId())
                        .params("signEntryTime", setParam(3))
                        .params("contractType", setParam(4))
                        .params("contractNature", dealText(xingzhiBean.getContent()))
                        .params("signedNumber", dealText(cishuBean.getContent()))
                        .params("lastReason", dealText(jieshuBean.getContent()))
                        .params("contractForm", dealText(xingshiBean.getContent()))
                        .params("contractStartTime", dealText(startTimeBean.getContent()))
                        .params("contractEndTime", dealText(endTimeBean.getContent()))
                        .params("probation", dealText(shiyongqiBean.getContent()))
                        .params("bank", dealText(bankBean.getContent()))
                        .params("ProbationSalary", salaryBean.getContent())
                        .execute(new DialogCallBack<HttpResult>(ApplyContractActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(setParam(0))) {
            msg = "请选择签订人";
            return false;
        }
        if (TextUtils.isEmpty(setParam(3))) {
            msg = "请选择入职时间";
            return false;
        }
        if (TextUtils.isEmpty(setParam(4))) {
            msg = "请选择合同类型";
            return false;
        }
        if (isFirstShow) {
            if (TextUtils.isEmpty(xingshiBean.getContent())) {
                msg = "请选择合同形式";
                return false;
            }
            if (TextUtils.isEmpty(shiyongqiBean.getContent())) {
                msg = "请选择试用期";
                return false;
            }
        }
        if (isSecondShow && TextUtils.isEmpty(cishuBean.getContent())) {
            msg = "请选择签订次数";
            return false;
        }
        if (isSecondShow && isThirdShow && TextUtils.isEmpty(jieshuBean.getContent())) {
            msg = "请选择上次合同结束时间";
            return false;
        }
        if (TextUtils.isEmpty(startTimeBean.getContent())) {
            msg = "请选择合同开始时间";
            return false;
        }
        if (TextUtils.isEmpty(endTimeBean.getContent())) {
            msg = "请选择合同结束时间";
            return false;
        }
        if (TextUtils.isEmpty(bankBean.getContent())) {
            msg = "请选择工资发放银行";
            return false;
        }
        if (TextUtils.isEmpty(salaryBean.getContent())) {
            msg = "请输入实习工资";
            return false;
        }

        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 0:
                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                break;
            case 4:
                if (leixingDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 19,
                            sheetDialog -> {
                                leixingDialog = sheetDialog;
                                leixingDialog.show();
                            }
                            , (position, strings) -> {
                                slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                                slimAdapterEx.notifyItemChanged(p);
                                if (position == 1 && !isFirstShow) {
                                    isFirstShow = true;
                                    slimAdapterEx.getData().add(p + 1, xingshiBean);
                                    slimAdapterEx.getData().add(p + 2, shiyongqiBean);
                                    slimAdapterEx.notifyItemRangeInserted(p + 1, 2);
                                } else if (position > 1 && isFirstShow) {
                                    isFirstShow = false;
                                    int removeFromPos = slimAdapterEx.getData().indexOf(xingshiBean);
                                    slimAdapterEx.getData().remove(xingshiBean);
                                    slimAdapterEx.getData().remove(shiyongqiBean);
                                    slimAdapterEx.notifyItemRangeRemoved(removeFromPos, 2);
                                }
                            }
                    );
                } else {
                    leixingDialog.show();
                }
                break;
            case 5:
                if (xingshiDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 9,
                            sheetDialog -> {
                                xingshiDialog = sheetDialog;
                                xingshiDialog.show();
                            }
                            , (position, strings) -> {
                                slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                                slimAdapterEx.notifyItemChanged(p);
                            }
                    );
                } else {
                    xingshiDialog.show();
                }
                break;
            case 6:
                if (shiyongqiDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 9,
                            sheetDialog -> {
                                shiyongqiDialog = sheetDialog;
                                shiyongqiDialog.show();
                            }
                            , (position, strings) -> {
                                slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                                slimAdapterEx.notifyItemChanged(p);
                            }
                    );
                } else {
                    shiyongqiDialog.show();
                }
                break;
            case 7:
                if (xingzhiDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 12,
                            sheetDialog -> {
                                xingzhiDialog = sheetDialog;
                                xingzhiDialog.show();
                            }
                            , (position, strings) -> {
                                slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                                slimAdapterEx.notifyItemChanged(p);
                                if (position >= 2 && !isSecondShow) {
                                    isSecondShow = true;
                                    if (isThirdShow) {
                                        slimAdapterEx.getData().add(p + 1, cishuBean);
                                        slimAdapterEx.getData().add(p + 2, jieshuBean);
                                        slimAdapterEx.notifyItemRangeInserted(p + 1, 2);
                                    } else {
                                        slimAdapterEx.getData().add(p + 1, cishuBean);
                                        slimAdapterEx.notifyItemInserted(p + 1);
                                    }
                                } else if (position == 1 && isSecondShow) {
                                    isSecondShow = false;
                                    if (isThirdShow) {
                                        slimAdapterEx.getData().remove(cishuBean);
                                        slimAdapterEx.getData().remove(jieshuBean);
                                        slimAdapterEx.notifyItemRangeRemoved(p + 1, 2);
                                    } else {
                                        slimAdapterEx.getData().remove(cishuBean);
                                        slimAdapterEx.notifyItemRemoved(p + 1);
                                    }
                                }
                            }
                    );
                } else {
                    xingzhiDialog.show();
                }
                break;
            case 8:
                if (cishuDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 17,
                            sheetDialog -> {
                                cishuDialog = sheetDialog;
                                cishuDialog.show();
                            }
                            , (position, strings) -> {
                                slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                                slimAdapterEx.notifyItemChanged(p);
                                if (position >= 2 && !isThirdShow) {
                                    isThirdShow = true;
                                    slimAdapterEx.getData().add(p + 1, jieshuBean);
                                    slimAdapterEx.notifyItemInserted(p + 1);
                                } else if (position == 1 && isThirdShow) {
                                    isThirdShow = false;
                                    slimAdapterEx.getData().remove(jieshuBean);
                                    slimAdapterEx.notifyItemRemoved(p + 1);
                                }
                            }
                    );
                } else {
                    cishuDialog.show();
                }
                break;
            case 9:
                if (jieshuDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 18,
                            sheetDialog -> {
                                jieshuDialog = sheetDialog;
                                jieshuDialog.show();
                            }
                            , (position, strings) -> {
                                slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                                slimAdapterEx.notifyItemChanged(p);
                            }
                    );
                } else {
                    jieshuDialog.show();
                }
                break;

            case 12:
                if (bankDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 35,
                            sheetDialog -> {
                                bankDialog = sheetDialog;
                                bankDialog.show();
                            }
                            , (position, strings) -> {
                                slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                                slimAdapterEx.notifyItemChanged(p);
                            }
                    );
                } else {
                    bankDialog.show();
                }
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditEnterBean("签订人", "", 0, true, "请选择"));
        datas.add(new SlimEditHintBean("所在部门", "", 1));
        datas.add(new SlimEditHintBean("所在岗位", "", 2));
        datas.add(new SlimEditTimeBean("入职时间", "", 3, "请选择"));
        datas.add(new SlimEditEnterBean("合同类型", "", 4, true, "请选择"));
        xingshiBean = new SlimEditEnterBean("合同形式", "", 5, true, "请选择");
        shiyongqiBean = new SlimEditEnterBean("试用期", "", 6, true, "请选择");
        xingzhiBean = new SlimEditEnterBean("合同性质", "", 7, true, "请选择");
        datas.add(xingzhiBean);
        cishuBean = new SlimEditEnterBean("合同签订次数", "", 8, true, "请选择");
        jieshuBean = new SlimEditEnterBean("上次合同结束原因", "", 9, true, "请选择");
        startTimeBean = new SlimEditTimeBean("合同开始时间", "", 10, "请选择");
        datas.add(startTimeBean);
        endTimeBean = new SlimEditTimeBean("合同结束时间", "", 11, "请选择");
        datas.add(endTimeBean);
        bankBean = new SlimEditEnterBean("工资发放银行", "", 12, true, "请选择");
        datas.add(bankBean);
        salaryBean = new SlimEditEnterBean("实习工资", "", 13, "请输入").addSchemes(StaticScheme.Numeric()).setTip("实习工资(单位元)");
        datas.add(salaryBean);

    }

    @Override
    protected void loadNetData() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Config.SELECT_SINGLE_STAFF) {
            if (resultCode == Config.INTENT_CONTENT) {
                bean = (EmploymentListResultBean) data.getSerializableExtra("bean");
                slimAdapterEx.getDataItem(0).setContent(bean.getStaffName());
                slimAdapterEx.getDataItem(1).setContent(bean.getDepartName());
                slimAdapterEx.getDataItem(2).setContent(bean.getPostName());
                slimAdapterEx.notifyItemRangeChanged(0, 3);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
